/**
 * 
 */
package com.ruyipu.forum.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.ruyipu.forum.R;
import com.ruyipu.forum.adapter.DiscussionAdapter;
import com.ruyipu.forum.base.Factory;
import com.ruyipu.forum.base.GlobalUtil;
import com.ruyipu.forum.base.Header;
import com.ruyipu.forum.interfaces.IItem;
import com.ruyipu.forum.item.Discussion;
import com.ruyipu.forum.model.DiscussionModel;

/**
 * @author donald
 * 
 */
public class AllDiscussions extends BaseActivity {

	private DiscussionAdapter myAdapter;
	private List<IItem> dataList = new ArrayList<IItem>();
	private ListView lvDiscussions;

	public static final String TAG = AllDiscussions.class.getSimpleName();
	public static final String NAME = AllDiscussions.class.getName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.alldiscussions);

		super.onCreate(savedInstanceState);

		refresh(null);
	}

	@Override
	protected void makeWidget() {
		myHeader = new Header(this, R.string.all_discussions);

		lvDiscussions = (ListView) findViewById(R.id.lvDiscussions);
	}

	@Override
	public void refresh(View v) {
		super.refresh(v);
		new MyDownloader().execute("");
	}

	private class MyDownloader extends AsyncDownloader {

		@Override
		protected JSONObject doInBackground(Object... params) {
			return DiscussionModel.getInstance().getDiscussions(
					AllDiscussions.this, 30, 1);
		}

	}

	@Override
	protected void initWidget() {
		lvDiscussions.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				GlobalUtil.viewDiscussion(AllDiscussions.this,
						(Discussion) dataList.get(arg2));
			}
		});
	}

	@Override
	protected void updateData(JSONObject jsonObject) {
		if (null == jsonObject) {
			return;
		}
		List<IItem> tempList = Factory.factoryItemList(jsonObject,
				Discussion.NAME);
		boolean dataChanged = false;
		while (!tempList.isEmpty()) {
			Discussion newDiscussion = (Discussion) tempList.get(0);
			boolean dataExist = false;
			for (int i = 0; i < dataList.size(); i++) {
				Discussion oldDiscussion = (Discussion) dataList.get(i);
				if (newDiscussion.getId() == oldDiscussion.getId()) {
					dataExist = true;
					if (!newDiscussion.getContent().equals(
							oldDiscussion.getContent())) {
						dataChanged = true;
						dataList.set(i, newDiscussion);
					}
					break;
				}
			}
			if (!dataExist) {
				dataChanged = true;
				dataList.add(newDiscussion);
			}
			tempList.remove(0);
		}
		if (dataChanged) {
			if (null == myAdapter) {
				myAdapter = new DiscussionAdapter(this, dataList);
				lvDiscussions.setAdapter(myAdapter);
				lvDiscussions.setLayoutAnimation(lacList);
			} else {
				myAdapter.notifyDataSetChanged();
			}
		}
	}

	@Override
	protected void loadLocalData() {
		JSONObject jsonObject = DiscussionModel.getInstance()
				.loadLocalDiscussions(30, 1);
		updateData(jsonObject);
	}
}